local wuhe = fk.CreateSkill{
  name = "hanqing__wuhe",
}

Fk:loadTranslationTable{
  ["hanqing__wuhe"] = "乌合",
  [":hanqing__wuhe"] = "每阶段限一次，你可以明置至多三张手牌以视为使用一张无距离限制的【杀】或令一张【杀】无效。"..
  "若你有明置手牌，这些牌不计入手牌上限且你不能发动〖乌合〗或使用暗置手牌。",

  ["#hanqing__wuhe"] = "乌合：你可以明置至多三张手牌，视为使用一张无距离限制的【杀】",
  ["#hanqing__wuhe-invoke"] = "乌合：你可以明置至多三张手牌，令 %dest 使用的%arg无效",
}

local DIY = require "packages/diy_utility/diy_utility"

wuhe:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#hanqing__wuhe",
  card_filter = function (self, player, to_select, selected)
    return #selected < 3 and not table.contains(DIY.getShownCards(player), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards == 0 or #cards > 3 then return end
    local card = Fk:cloneCard("slash")
    card.skillName = wuhe.name
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    DIY.showCards(player, use.card.fake_subcards)
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(wuhe.name, Player.HistoryPhase) == 0 and #DIY.getShownCards(player) == 0
  end,
  enabled_at_response = function (self, player, response)
    return not response and player:usedSkillTimes(wuhe.name, Player.HistoryPhase) == 0 and #DIY.getShownCards(player) == 0
  end
})

wuhe:addEffect(fk.CardUsing, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(wuhe.name) and #DIY.getShownCards(player) == 0 and
      data.card.trueName == "slash" and
      player:usedSkillTimes(wuhe.name, Player.HistoryPhase) == 0 and
      #table.filter(player:getCardIds("h"), function (id)
        return not table.contains(DIY.getShownCards(player), id)
      end) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = DIY.askToShowCards(player, {
      min_num = 1,
      max_num = 3,
      skill_name = wuhe.name,
      prompt = "#hanqing__wuhe-invoke::"..target.id..":"..data.card:toLogString(),
      cancelable = true,
      skip = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards
    DIY.showCards(player, cards)
    data.nullifiedTargets = table.simpleClone(player.room.players)
  end,
})

wuhe:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and table.contains(card.skillNames, wuhe.name)
  end
})

wuhe:addEffect("maxcards", {
  exclude_from = function (self, player, card)
    return player:hasSkill(wuhe.name) and table.contains(DIY.getShownCards(player), card.id)
  end,
})

wuhe:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if player:hasSkill(wuhe.name) and #DIY.getShownCards(player) > 0 and card then
      return table.find(Card:getIdList(card), function (id)
        return not table.contains(DIY.getShownCards(player), id)
      end)
    end
  end,
})

wuhe:addAI(nil, "vs_skill")

return wuhe
